package com.yatuhashi.manual.mhp3.async;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.os.AsyncTask;
import android.preference.PreferenceManager;

import com.yatuhashi.manual.mhp3.SplashActivity;
import com.yatuhashi.manual.mhp3.parse.MonsterParser;
import com.yatuhashi.manual.mhp3.parse.QuestDetailParser;
import com.yatuhashi.manual.mhp3.parse.QuestParser;

public class InitDatabaseTask extends AsyncTask<Void, Integer, Boolean>{
    private Context mContext;
    private SplashActivity mActivity;
    private ProgressDialog mDialog;
    private static final int MAX_XML_NUM = 3;
    
    public InitDatabaseTask(Context context) {
        mContext = context;
        mActivity = (SplashActivity) mContext;
    }
    
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mDialog = new ProgressDialog(mContext);
        mDialog.setTitle("データベース初期化");
        mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mDialog.setCancelable(true);
        mDialog.show();
    }
    
    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        mDialog.setProgress(values[0]);
        mDialog.setMax(values[1]);
    }
    
    @Override
    protected Boolean doInBackground(Void... params) {
        boolean success = false;
        
        publishProgress(0, MAX_XML_NUM);
        
        // 処理書き込み
        try {
            AssetManager am = mContext.getResources().getAssets();
            QuestParser qparser = new QuestParser(mContext);
            qparser.parse(am.open("quest.xml", AssetManager.ACCESS_STREAMING));
            publishProgress(1, MAX_XML_NUM);
            
            QuestDetailParser qdparser = new QuestDetailParser(mContext);
            qdparser.parse(am.open("quest_detail.xml", AssetManager.ACCESS_STREAMING));
            publishProgress(2, MAX_XML_NUM);
            
            MonsterParser mparser = new MonsterParser(mContext);
            mparser.parse(am.open("monster.xml", AssetManager.ACCESS_STREAMING));
            publishProgress(3, MAX_XML_NUM);
            
            success = true;
        } catch (IOException e) {
            success = false;
        } catch (Exception e) {
            success = false;
        }
        
        return success;
    }
    
    public static String inputStreemToString(InputStream in) throws IOException{
        BufferedReader reader = 
            new BufferedReader(new InputStreamReader(in, "UTF-8"/* 文字コード指定 */));
        StringBuffer buf = new StringBuffer();
        String str;
        while ((str = reader.readLine()) != null) {
                buf.append(str);
                buf.append("\n");
        }
        return buf.toString();
    }
    
    @Override
    protected void onCancelled() {
        super.onCancelled();
    }
    
    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        mDialog.dismiss();
        if(result) {
            SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mContext);
            SharedPreferences.Editor editor = pref.edit();
            editor.putBoolean("initialize", true);
            editor.commit();
        } else {
            
        }
        mActivity.startAnimation();
    }
}
